package cn.oveay.classdesign.controller.servlet;

import cn.oveay.classdesign.bean.basebean.User;
import cn.oveay.classdesign.service.UserService;
import cn.oveay.classdesign.service.impl.UserServiceImpl;
import cn.oveay.classdesign.util.commons.Nanoflake;
import cn.oveay.classdesign.util.security.MD5;
import cn.oveay.classdesign.util.security.SHA;
import cn.oveay.classdesign.util.servlet.BaseServlet;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/register")
public class RegisterServlet extends BaseServlet {
    private UserService userService = new UserServiceImpl();

    public String forward(HttpServletRequest req, HttpServletResponse resp){
        return "/WEB-INF/pages/register.html";
    }

    public String register(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String name = req.getParameter("name");
        String pwd = req.getParameter("pwd");
        String confirmPwd = req.getParameter("confirmPwd");
        if(checkPassword(pwd, confirmPwd) != 0 || checkUsername(name) != 0){
            resp.getWriter().append("{\"error\":true, \"msg\":\"用户名或密码出现错误.\"}");
            return null;
        }
        User user = new User(null, name, pwd, null, null);
        userService.inseartUser(user);
        resp.getWriter().append("{\"error\":false}");
//        return "/login?method=forward";
        return null;
    }

    public String checkUsername(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String name = req.getParameter("name");
        Integer flag = checkUsername(name);
        switch (flag){
            case 0:
                return null;
            case 1:
                resp.getWriter().append("{\"error\":true, \"msg\":\"用户名不能为空.\"}");
                return null;
            case 2:
                resp.getWriter().append("{\"error\":true, \"msg\":\"用户名已经存在.\"}");
                return null;
            default:
                resp.getWriter().append("{\"error\":true, \"msg\":\"未知错误.\"}");
        }
        return null;
    }

    private Integer checkUsername(String name){
        if("".equals(name.trim())){
            return 1;
        }
        if(userService.findByName(name) != null){
            return 2;
        }
        return 0;
    }

    public String checkPassword(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String pwd = req.getParameter("pwd");
        String confirmPwd = req.getParameter("confirmPwd");
        Integer flag = checkPassword(pwd, confirmPwd);
        switch (flag){
            case 0:
                return null;
            case 1:
                resp.getWriter().append("{\"error\":true, \"msg\":\"密码不能为空.\"}");
                return null;
            case 2:
                resp.getWriter().append("{\"error\":true, \"msg\":\"确认密码不能为空.\"}");
                return null;
            case 3:
                resp.getWriter().append("{\"error\":true, \"msg\":\"两次密码不一致.\"}");
                return null;
            default:
                resp.getWriter().append("{\"error\":true, \"msg\":\"未知错误.\"}");
        }
        return null;
    }

    private Integer checkPassword(String pwd, String confirmPwd){
        if("".equals(pwd.trim())){
            return 1;
        }
        if("".equals(confirmPwd.trim())){
            return 2;
        }
        if(!pwd.equals(confirmPwd)){
            return 3;
        }
        return 0;
    }
}
